package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.GroupMemberDao;
import com.zja.model.GroupMember;

import java.util.List;

/**
 * @author JA
 */
public class GroupMemberDaoImpl extends BaseDAO implements GroupMemberDao {
    @Override
    public List<GroupMember> selectAll() {
        String sql = "SELECT * FROM group_member";
        try {
            return executeQuery(GroupMember.class, sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public GroupMember selectById(Long groupId, Long userId) {
        String sql = "SELECT * FROM group_member WHERE group_id = ? AND user_id = ?";
        try {
            return executeQueryBean(GroupMember.class, sql, groupId, userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(GroupMember groupMember) {
        String sql = "INSERT INTO group_member(group_id, user_id, role, joined_at) VALUES(?, ?, ?, ?)";
        try {
            return executeUpdate(sql, groupMember.getGroupId(), groupMember.getUserId(), groupMember.getRole(), groupMember.getJoinedAt());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(GroupMember groupMember) {
        String sql = "UPDATE group_member SET role = ?, joined_at = ? WHERE group_id = ? AND user_id = ?";
        try {
            return executeUpdate(sql, groupMember.getRole(), groupMember.getJoinedAt(), groupMember.getGroupId(), groupMember.getUserId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long groupId, Long userId) {
        String sql = "DELETE FROM group_member WHERE group_id = ? AND user_id = ?";
        try {
            return executeUpdate(sql, groupId, userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
